//
//  JSDSportMappingViewController.m
//  仿咕咚
//
//  Created by Abner on 16/11/8.
//  Copyright © 2016年 Abner. All rights reserved.
//

#import "JSDSportMappingViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "JSDSportTrackingLine.h"
@interface JSDSportMappingViewController ()<MAMapViewDelegate>

@end

@implementation JSDSportMappingViewController{
    //起始位置
    CLLocation *_startLocation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMap];
}
- (void)addMap{
    ///初始化地图
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    mapView.showsScale = NO;
    mapView.showsUserLocation = YES;
    //禁止相机旋转，减少性能消耗
    //    mapView.rotateCameraEnabled = NO;
    //可以更新用户的location，是用户的位置一开始就在屏幕中心
    mapView.userTrackingMode = MAUserTrackingModeFollow;
    mapView.allowsBackgroundLocationUpdates = YES;
    
    ///把地图添加至view
    [self.view addSubview:mapView];
    //设置代理
    mapView.delegate = self;
    
}
//实时更新位置
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if (!updatingLocation) {
        return;
    }
    if (_startLocation == nil) {
        _startLocation = userLocation.location;
        MAPointAnnotation *annotation = [MAPointAnnotation new];
        annotation.coordinate = userLocation.location.coordinate;
        [mapView addAnnotation:annotation];
    }
    [mapView addOverlay:[_sportTracking appendLocation:userLocation.location]];
}
//自定义大头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{
    //判断大头针的类型
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        return nil;
    }
    //查询可重用大头针
    static NSString *annotationViewID = @"annotationViewID";
    MAAnnotationView *annotationV = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
    if (annotationV == nil) {
        //如果没有新建
        annotationV = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID];
    }
    //有就直接赋值
    UIImage *image = [UIImage imageNamed:_sportTracking.imageStr];
    annotationV.image = image;
    annotationV.centerOffset = CGPointMake(0, - image.size.height * 0.5);
    return annotationV;
}

/**
 点击地图绘制折线

 @param mapView    地图
 @param coordinate 经纬度坐标
 */
-(void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{
    //1、建立结构体数组
    CLLocationCoordinate2D coords[2];
    coords[0] = coordinate;
    coords[1] = _startLocation.coordinate;
    //2、构造折线对象
    MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:coords count:2];
    //3、添加到视图上为遮罩层
    [mapView addOverlay:polyLine];
}

/**
 渲染折线

 @param mapView 地图
 @param overlay 折线遮罩层

 @return 地图覆盖物的基类
 */
-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay{
    //判断overlay的类型
    if (![overlay isKindOfClass:[MAPolyline class]]) {
        return nil;
    }
    JSDSportMAPolyline *polyLine = (JSDSportMAPolyline *)overlay;
    //实例化MAOverlayRenderer
    MAPolylineRenderer *render = [[MAPolylineRenderer alloc] initWithOverlay:overlay];
    render.lineWidth = 5;
    render.strokeColor = polyLine.color;
    
    return render;
}
@end
